/* 
 * Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *      * The names of its contributors may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kameleon.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import kameleon.exception.FileReadingException;
import kameleon.exception.FileWritingException;
import kameleon.exception.InvalidConfigurationFileException;
import kameleon.plugin.PlugInInfo;


/**
 * Utility class for writing and reading objects to files.
 * 
 * @author		Schnell Michaël
 * @version		1.0	
 */
public class IOObject {

	/**
	 * Returns the first object read from the given file.
	 * 
	 * @param 	filePath
	 * 			absolute path of the source file
	 * 
	 * @return	First object read from the source file
	 * 
	 * @throws 	FileReadingException
	 * 			if an error occurred while reading the file
	 */
	public static Object readObjectFromFile(String filePath) throws FileReadingException {
		File sourceFile = new File(filePath) ;
		try {
			return readObjectFromFile(new FileInputStream(sourceFile)) ;
		} catch (IOException e) {
			throw new FileReadingException(sourceFile) ;
		}// try
	}// readObjectFromFile(String)

	/**
	 * Returns the first object read from the given {@code InputStream}.
	 * 
	 * @param 	in
	 * 			source {@code InputStream}
	 * 
	 * @return	First object read from the source {@code InputStream}
	 * 
	 * @throws 	FileReadingException
	 * 			if an error occurred while reading the {@code InputStream}
	 */
	public static Object readObjectFromFile(InputStream in) throws FileReadingException {
		try {
			ObjectInputStream oin = new ObjectInputStream(in) ;
			return oin.readObject() ;
		} catch (IOException e) {
			throw new FileReadingException() ;
		} catch (ClassNotFoundException e) {
			//TODO Find better solution ?
			throw new FileReadingException() ;
		}// try
	}// readObjectFromFile(InputStream)

	/**
	 * Writes the given {@code Object} the the given file. If the file already exists, 
	 * it is rewritten.
	 * 
	 * @param 	filePath
	 * 			absolute path the target file
	 * 
	 * @param 	object
	 * 			object written to the file
	 * 
	 * @throws 	FileWritingException
	 * 			if an error occurred while writing
	 */
	public static void writeObjectToFile(String filePath, Object object) 
			throws FileWritingException {
		File target = new File(filePath) ;
		try {
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(target)) ;
			out.writeObject(object) ;
			out.close() ;
		} catch (IOException ex) {
			throw new FileWritingException(target) ;
		}// try
	}// writeObjectToFile(String, Object)

	/**
	 * Reads the given file and returns the read instance of {@code List<PlugInInfo>}.
	 * 
	 * @param 	source
	 * 			absolute path of the source file
	 * 
	 * @return	Instance of {@code List<PlugInInfo>} read from the given file
	 * 
	 * @throws  FileReadingException
	 * 			if the given file could not be read
	 * 	
	 * @throws 	InvalidConfigurationFileException
	 * 			if the file did not contain a valid list of {@code PlugInInfo}
	 */
	@SuppressWarnings("unchecked")
	public static List<PlugInInfo> readList(String source) 
			throws FileReadingException, InvalidConfigurationFileException {
		Object obj = IOObject.readObjectFromFile(source) ;
		if (!(obj instanceof List<?>)) {
			throw new InvalidConfigurationFileException(new File(source)) ;
		}// if
		List<?> readList = (List<?>) obj ;
		if (!readList.isEmpty() && !(readList.get(0) instanceof PlugInInfo)) {
			throw new InvalidConfigurationFileException(new File(source)) ;
		}// if
		return (List<PlugInInfo>) readList ;
	}// readList(String, List<PlugInInfo>)

}// class IOObject